// pages/resource/resource.js

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    resourceList: [],
    songList: [],
    quota: 0,
    resourceCurPage: 1,
    resourceTotalPage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    app.getUserInfo(this.refreshQuota)

    this.refreshResource(1);
    //this.refreshSongList();
    //this.refreshQuota();
    //this.refreshvideoList()
  },

  refreshvideoList() {
    //调后端接口获取token
    app.apiRequest({
      url: '/BanyunApi/queryVideoList',
      method: 'GET',
      data: {
        'token': "acefxaf"
      },
      success: res => {
        console.log("refreshvideoList success")
        this.setData({
          nodeList: res.data.data
        })
      }
    });
  },


  refreshSongList() {

    //调后端接口获取token
    app.apiRequest({
      url: '/songList',
      method: 'GET',
      data: {
        'token': "acefxaf"
      },
      success: res => {
        console.log("refreshSongList success")
        this.setData({
          songList: res.data.data
        })
      }
    });
  },
  getUserProfile() {

  },

  refreshResource(pageNumber) {
    this.setData({
      resourceCurPage: pageNumber
    })

    //调后端接口获取token
    app.apiRequest({
      url: '/liveListByPage?pageSize=8&pageNumber=' + pageNumber,
      method: 'GET',
      data: {
        'token': "acefxaf"
      },
      success: res => {
        console.log("refreshResource success")
        this.setData({
          resourceList: res.data.data,
          resourceCurPage: res.data.currentPage,
          resourceTotalPage: res.data.totalPage
        })
      }
    });
  },

  refreshQuota(res) {

    console.log("call refreshQuota success")
    console.log(res)


    //调后端接口获取额度
    app.apiRequest({
      url: '/TKApi/getActionQuota',
      method: 'GET',
      data: {
        'userId': app.globalData.userId
      },
      success: res => {
        console.log("refreshQuota success")
        console.log(res)
        if (res.data.success == true) {
          this.setData({
            quota: res.data.data.quota
          })
        }

      }
    });
  },

  consumeQuota(type, actionId) {
    //消费额度
    app.apiRequest({
      url: '/consumeQuota',
      method: 'GET',
      data: {
        'userId': app.globalData.userId,
        'type': type,
        'actionId': actionId
      },
      success: res => {
        console.log("consumeQuota success")
        this.refreshQuota();
      }
    });
  },
  rewardQuota() {

    console.log("rewardQuota call")

    //奖励额度
    app.apiRequest({
      url: '/rewardQuota',
      method: 'GET',
      data: {
        'userId': app.globalData.userId
      },
      success: res => {
        console.log("rewardQuota success")
        console.log(res)

        this.refreshQuota();
      }
    });
  },

  live_prev() {
    if (this.data.resourceCurPage <= 1) {
      return;
    }
    this.refreshResource(this.data.resourceCurPage - 1)
  },

  live_next() {
    if (this.data.resourceCurPage >= this.data.resourceTotalPage) {
      return;
    }
    this.refreshResource(this.data.resourceCurPage + 1)
  },

  showShareInfo(e) {

    if (this.data.quota <= 0) {
      console.log("查看次数已经用完，转发给朋友可以奖励查看次数")

      wx.showToast({
        title: "查看次数已经用完，转发小程序奖励查看次数",
        icon: "none",
        duration: 2e3
      });

      return;
    }

    console.log("showShareInfo success")
    console.log(e)
    wx.showModal({
      content: e.currentTarget.dataset.msg,
      confirmText: "复制",
      success(res) {
        if (res.confirm) {

          wx.setClipboardData({
            data: e.currentTarget.dataset.msg,
            success: function (res) {
              console.log('复制成功')


            },
            fail: function (res) {
              console.log('复制失败')

            }
          });

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

    this.consumeQuota(1, e.currentTarget.dataset.id)

  },



  playMusic(e) {
    if (this.data.quota <= 0) {
      console.log("播放次数已经用完，转发给朋友可以奖励播放次数")
      wx.showToast({
        title: "播放次数已经用完，转发小程序奖励播放次数",
        icon: "none",
        duration: 2e3
      });
      return;
    }


    console.log("playMusic success")
    console.log(e)
    app.globalData.currentIndex = e.currentTarget.dataset.index
    app.globalData.songList = this.data.songList
    app.globalData.newSong = 1;
    console.log(app.globalData.songList)
    wx.setStorageSync('songList', this.data.songList)
    // wx.switchTab({
    //   url: '/pages/player/player'
    // })

    this.consumeQuota(2, e.currentTarget.dataset.id)


    wx.navigateTo({
      url: "../player/player",
    })

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

    console.log("onShareAppMessage call")
    this.rewardQuota()

    return {
      title: '推荐一个好玩的站点，有超多财经、金融、人文类信息，供大家交流学习，超级好用',
      path: '/pages/index/index',
      imageUrl: '/images/share.jpg'


    }
  }
})